package com.zycfc.zsf.boot.util.text;

import java.util.regex.*;
import java.util.*;
import org.slf4j.*;

public class PhoneValidator
{
    private static final Logger log;
    private static Map<String, String> phoeList;
    
    public static boolean isMobile(final String str) {
        return isMobile(false, str);
    }
    
    public static boolean isMobile(final boolean isNull, final String str) {
        if (StringUtils.isEmptyAll(str) && isNull) {
            return true;
        }
        if (str.length() != 11) {
            return false;
        }
        final Pattern p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
        final Matcher m = p.matcher(str);
        return m.matches();
    }
    
    private static Map<String, String> loadPhoneList() {
        final Map<String, String> words = new HashMap<String, String>();
        final String path = "classpath:config/zsf/boot/util/phone.txt";
        ResourceLoaderUtils.load(new ResourceLoader() {
            @Override
            public void clear() {
            }
            
            @Override
            public void load(final List<String> lines) {
                for (final String str : lines) {
                    words.put(str, str);
                }
            }
        }, path);
        return words;
    }
    
    private static boolean isPhoneOld(final String str) {
        if (StringUtils.isEmptyAll(str)) {
            return true;
        }
        Pattern p1 = null;
        Pattern p2 = null;
        Matcher m = null;
        boolean b = false;
        p1 = Pattern.compile("^[0-9]{0,4}-{0,1}[0-9]{5,10}$");
        p2 = Pattern.compile("^[0-9]{1}[0-9]{5,8}$");
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        }
        else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;
    }
    
    public static boolean isPhone(final String str) {
        if (StringUtils.isEmptyAll(str)) {
            return true;
        }
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        if (!str.contains("-")) {
            p = Pattern.compile("^[0-9]{7,16}$");
            m = p.matcher(str);
            b = m.matches();
            return b;
        }
        final String[] strs = str.split("-");
        final int count = strs.length - 1;
        if (count == 1) {
            p = Pattern.compile("^[0-9]{3,12}-{1}[0-9]{1,8}$");
            m = p.matcher(str);
            b = m.matches();
        }
        if (count == 2) {
            p = Pattern.compile("^[0-9]{3,4}-{1}[0-9]{7,8}-{1}[0-9]{1,4}$");
            m = p.matcher(str);
            b = m.matches();
        }
        return b;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)PhoneValidator.class);
        PhoneValidator.phoeList = null;
    }
}
